﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using RSSReader.MobileClient.Resources;
using RSSReader.MobileClient.ViewModel;
using RSSReader.MobileClient.View;
using RSSReader.MobileClient.Model;
using RSSReader.MobileClient.WCFWebRoleService;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;

namespace RSSReader.MobileClient
{
    public partial class FeedSelectorPage : PhoneApplicationPage
    {
        private FeedViewModel Feedvm;
        DataDistributorClient client = new DataDistributorClient();
        bool feedstatus = false;

        public FeedSelectorPage()
        {
            InitializeComponent();
            //NavigationService.RemoveBackEntry();
            Feedvm = new FeedViewModel();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NetworkInterface.GetIsNetworkAvailable())
            {
                var asyncMgr = new AsyncDataManager(() =>
                {
                    FeedSelectorOnPage.DataContext = Feedvm;
                });

                asyncMgr.SetExpectation("GotSubscriptions");
                Feedvm.GetFeeds(asyncMgr);
            }
            // Set the data context for the Level view.
            //LevelViewOnPage.DataContext = from Accomplishment in vm.Accomplishments where Accomplishment.Type == "Level" select Accomplishment;*/

            // If there is only one view, you could use the following code
            // to populate the view.
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);

            MessageBoxResult MsgBR = MessageBox.Show("Do you want to quit the application ?", "Leave", MessageBoxButton.OKCancel);
            if (MsgBR == MessageBoxResult.OK)
            {
                //Ne pas oublier d'ajouter le code de sauvegarde ici
                Application.Current.Terminate();
            }
            else
            {
                e.Cancel = true;
                //Feedvm.Feeds.Add(new Feed() { Link = "/ArticlePage.xaml?feedorigin=All", FeedName = "Works", UnreadNb = 20 });
                FeedSelectorOnPage.InitializeComponent();
                return;
            }
        }

        private void AddFeedB_Click(object sender, RoutedEventArgs e)
        {
            if (!feedstatus)
            {
                NewFeedUrl.Visibility = System.Windows.Visibility.Visible;
                feedstatus = true;
            }
            else
            {
                if (NewFeedUrl.Text != "")
                {
                    ObservableCollection<WCFWebRoleService.UserSubscriptionInformation> NewFeeds = new ObservableCollection<WCFWebRoleService.UserSubscriptionInformation>();
                    WCFWebRoleService.UserSubscriptionInformation NewFeed = new WCFWebRoleService.UserSubscriptionInformation();
                    NewFeed.Url = NewFeedUrl.Text;
                    NewFeed.UserId = App.UserId;
                    NewFeeds.Add(NewFeed);
                    var asyncMgr = new AsyncDataManager(() =>
                    {
                        if (Feedvm.LegitFeed)
                        {
                            Feedvm.AddFeed(NewFeeds);
                            NewFeedUrl.Visibility = System.Windows.Visibility.Collapsed;
                            //vigationService.Navigate(new Uri("/FeedSelectorPage.xaml",UriKind.Relative));
                            NavigationService.Navigate(new Uri("/MainPage.xaml?" + DateTime.Now.Ticks, UriKind.Relative));
                            feedstatus = false;
                            
                        }
                        else
                        {
                            MessageBox.Show("The url doesn't have a good format, or isn't a legit RSS feed");
                            Feedvm.LegitFeed = true;
                        }
                    });
                    asyncMgr.SetExpectation("CheckedFeed");
                    Feedvm.CheckFeedValidity(NewFeeds[0].Url, asyncMgr);
                }
                else
                    MessageBox.Show("You cannot add an empty feed");
            }
        }
    }
}